--[[ 雷鸟之翼
  直接用read_u16的方法即可.
  
  改装到MyMame模块和MySTG模块,Y坐标需要反转
  
  By setycyas @2025-10-10
]]

--防止重复运行
if (_G.game) then
  print('script already loaded!')
  return
end

local myMame = require('MyMame')
local myStg = require('MySTG')

-- 这里设定基本参数
local GAME = {
-- 脚本名,防重加载用
  NAME = "gigawing_autoboot",
  -- 分辨率
  X_RESOLUTION = 384,
  Y_RESOLUTION = 224,
  -- 横坐标纵坐标内存地址
  X_ADDR_HIGH = 0xffae6c,
  X_ADDR_LOW = 0xffae6d,
  Y_ADDR_HIGH = 0xffae6e,
  Y_ADDR_LOW = 0xffae6f,
  -- 坐标修正像素
  X_OFFSET = 0,
  Y_OFFSET = -5,
  -- 自己中心方块的半长半高,单位为MAME的屏幕比例
  --BOX_HALF_WIDTH = 0.005,
  --BOX_HALF_HEIGHT = 0.005,
  BOX_WIDTH = 4,
  BOX_HEIGHT = 4,
  -- 中心方块的边框颜色和中心颜色
  BOX_BORDER_COLOR = 0x8800ffff,
  BOX_COLOR = 0x8800cccc,
  -- 十字线宽度和颜色
  LINE_WIDTH = 1.15,
  LINE_COLOR = 0xFF0000ff,
  -- 中途需要变更的测试参数,准备了10个
  TEST_PARAMS = {64.0, 64.0, 208.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  -- mame api用的对象
  mem = manager.machine.devices[':maincpu'].spaces['program'],
  container = manager.machine.screens[":screen"].container,
  ui_container = manager.machine.render.ui_container,
}
-- 更新一下模块的参数
myMame.gameName = GAME.NAME
myMame.screenIndex = ':screen'
myStg.screenIndex = ':screen'
myStg.X_RESOLUTION = GAME.X_RESOLUTION
myStg.Y_RESOLUTION = GAME.Y_RESOLUTION

-- 坐标获取,返回整数,Y需要反转
function GAME.getX_u16()
  local result = -1
  if (GAME.X_ADDR_HIGH > 0) then
    result = myMame.mem:read_u16(GAME.X_ADDR_HIGH)
  end
  return result+GAME.X_OFFSET
end
function GAME.getY_u16()
  local result = -1
  if (GAME.Y_ADDR_HIGH > 0) then
    result = GAME.Y_RESOLUTION-myMame.mem:read_u16(GAME.Y_ADDR_HIGH)
  end
  return result+GAME.Y_OFFSET
end

-- 数据显示,指定位置和颜色,然后是可变的多个显示数据
function GAME.showInfo(position, color, ...)
  local count = select("#", ...)
  
  for i = 1, count do
    local arg = select(i, ...)
    GAME.ui_container:draw_text(position, 1.01-0.05*i, arg, color, 0xFF839496)
  end
  
end

-- 监视数据显示
function GAME.check()
  local xlow, ylow, xhigh, yhigh --内存数据
  yhigh = GAME.mem:read_u8(GAME.Y_ADDR_HIGH)
  xlow = GAME.mem:read_u8(GAME.X_ADDR_LOW)
  ylow = GAME.mem:read_u8(GAME.Y_ADDR_LOW)
  xhigh = GAME.mem:read_u8(GAME.X_ADDR_HIGH)
  -- 显示当前火力等级
  GAME.showInfo(0.05, 0xFF000000, 
    string.format(" XH=%d XL=%d YH=%d YL=%d", xhigh, xlow, yhigh, ylow)
  )
end

-- 显示自机的几个方框,xfloat,yfloat是浮点数坐标
-- 显示中心
function GAME.showCenter(x, y)

  myStg.centerBox(
    x, y,
    GAME.BOX_WIDTH, GAME.BOX_HEIGHT,
    GAME.BOX_BORDER_COLOR, GAME.BOX_COLOR
  )
  
end
  
-- 显示十字线
function GAME.showCrossLine(x, y)

  myStg.xLine(x, GAME.LINE_COLOR, GAME.LINE_WIDTH)
  myStg.yLine(y, GAME.LINE_COLOR, GAME.LINE_WIDTH) 
  
end

--[[ **************************
  主函数,绑定后每一帧执行一次该函数
]]
function GAME.Main()
  local x, y

  -- 获取整数坐标
  x = GAME.getX_u16()
  y = GAME.getY_u16()
  
  -- 数据监视
  --GAME.check()
  -- 显示角色中心辅助线
  if (x > 0) and (y > 0) then
    -- 十字线
    GAME.showCrossLine(x, y)
    -- 中心
    GAME.showCenter(x, y)
  end
  
end

--[[运行]]

-- 绑定每一帧执行的函数,测试时绑定到debug
--myMame.debugRunPerFrame(GAME.Main, 'gigawing')
myMame.runPerFrame(GAME.Main, 'gigawing')
-- 导出到全局变量
_G.game = GAME

return GAME
